import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 选素数
 * User: DELL
 * Date: 2024-05-24
 * Time: 9:03
 */
public class Main7 {
    static boolean[] vis = new boolean[1000001];    // 标记是否为素数 是--false 不是--true
    static int[] maxFac = new int[1000001]; // 记录最大质因子

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        // 初始化
        init();
        if (!vis[n]) {
            System.out.println(-1);
            return;
        }
        // 首先根据 n 的最大素数确定范围
        int max = maxFac[n];
        int left = n - max + 1;
        int right = n;
        int res = 1000001;
        for (int i = left; i <= right; i++) {
            if (!vis[i]) {
                continue;
            } else {
                res = Math.min(res, i - maxFac[i] + 1);
            }
        }
        if (res == 1000001) {
            System.out.println(-1);
        } else {
            System.out.println(res);
        }
    }

    private static void init() {
        vis[1] = true;
        vis[0] = true;
        for (int i = 2; i < 1000001; i++) {
            if (!vis[i]) {
                maxFac[i] = i;
            }
            for (int j = 2; i * j < 1000001; j++) {
                vis[i * j] = true;
                maxFac[i * j] = Math.max(maxFac[i], maxFac[j]);
            }
        }
    }
}
